//
//  LoadingView.m
//  YD_RRT
//
//  Created by yonyou on 15/5/23.
//  Copyright (c) 2015年 zgq. All rights reserved.
//

#import "LoadingView.h"
#import "borgword_define.h"
@implementation LoadingView
@synthesize activity=_activity;
@synthesize loadingLabel=_loadingLabel;
+ (LoadingView *)sharedView
{
    static LoadingView *s_loadview = nil;
    @synchronized(s_loadview)
    {
        if ([LoadingView invalid:s_loadview])
        {
            s_loadview = [[LoadingView alloc] init];
        }
        
        return s_loadview;
    }
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        //UIWindow *win=[UIApplication sharedApplication].keyWindow;
        
        self.backgroundColor = [UIColor clearColor];
        self.alpha=0.75f;
        
        
        // Initialization code
    }
    return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if(isCancel)
    {
        [self dismissFromSupview];
    }
}

-(void)loadingViewWithString:(NSString*)str superView:(UIView*)superview isCancel:(BOOL)isCa
{
    self.frame=superview.frame;
    isCancel=isCa;
    
    UIView *loadlabelview=[[UIView alloc]initWithFrame:CGRectMake(self.frame.size.width/2-120, self.frame.size.height/2-60, 240.0, 120.0)];
    loadlabelview.backgroundColor=[UIColor blackColor];
    loadlabelview.layer.borderWidth=1.0;
    loadlabelview.layer.borderColor=[UIColor clearColor].CGColor;
    loadlabelview.layer.masksToBounds = YES;
    loadlabelview.layer.cornerRadius = 5;
    [self addSubview:loadlabelview];
    // CGRect ffrm=loadlabelview.frame;
    self.loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 50, 160.0, 20.0)];
    self.loadingLabel.textColor = [UIColor whiteColor];
    //self.loadingLabel.numberOfLines=0;
    self.loadingLabel.backgroundColor = [UIColor clearColor];
    self.loadingLabel.textAlignment=NSTextAlignmentCenter;
    self.loadingLabel.font=[UIFont systemFontOfSize:14];
    self.loadingLabel.adjustsFontSizeToFitWidth=YES;
    self.loadingLabel.text = str;
    
    [loadlabelview addSubview:self.loadingLabel];
    self.activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(30,44, 32, 32)];
    
    [self.activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [loadlabelview addSubview:self.activity];
    [self.activity startAnimating];
    [superview addSubview:self];
}
-(void)loadingViewWithsuperViewToTop:(UIView*)superview
{
    self.frame=CGRectMake(0, self.window.frame.origin.y, UISCREEN_BOUNDS_WIDTH, UISCREEN_BOUNDS_HEIGHT);
    NSLog(@"%@",[NSValue valueWithCGRect:self.frame]);
    self.center=CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/2-64);
    UIView *loadlabelview=[[UIView alloc]initWithFrame:CGRectMake(UISCREEN_BOUNDS_WIDTH/2-60, UISCREEN_BOUNDS_HEIGHT/2-60, 120.0, 120.0)];
    //loadlabelview.center=
    loadlabelview.backgroundColor=[UIColor blackColor];
    loadlabelview.layer.borderWidth=1.0;
    loadlabelview.layer.borderColor=[UIColor clearColor].CGColor;
    loadlabelview.layer.masksToBounds = YES;
    loadlabelview.layer.cornerRadius = 5;
    [self addSubview:loadlabelview];
    // CGRect ffrm=loadlabelview.frame;
    self.loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 70, 80.0, 20.0)];
    self.loadingLabel.textColor = [UIColor whiteColor];
    //self.loadingLabel.numberOfLines=0;
    self.loadingLabel.backgroundColor = [UIColor clearColor];
    self.loadingLabel.textAlignment=NSTextAlignmentCenter;
    self.loadingLabel.font=[UIFont systemFontOfSize:14];
    //self.loadingLabel.adjustsFontSizeToFitWidth=YES;
    self.loadingLabel.text = @"正在加载...";
    
    [loadlabelview addSubview:self.loadingLabel];
    self.activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(45,20, 32, 32)];
    self.activity.center=CGPointMake(60, 35);
    
    [self.activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [loadlabelview addSubview:self.activity];
    [self.activity startAnimating];
    [superview addSubview:self];
}
-(void)loadingViewWithsuperView:(UIView*)superview
{
    
    self.frame=CGRectMake(0, self.window.frame.origin.y, UISCREEN_BOUNDS_WIDTH, UISCREEN_BOUNDS_HEIGHT);
    NSLog(@"%@",[NSValue valueWithCGRect:self.frame]);
    self.center=CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/2);
    UIView *loadlabelview=[[UIView alloc]initWithFrame:CGRectMake(UISCREEN_BOUNDS_WIDTH/2-60, UISCREEN_BOUNDS_HEIGHT/2-60, 120.0, 120.0)];
    //loadlabelview.center=
    loadlabelview.backgroundColor=[UIColor blackColor];
    loadlabelview.layer.borderWidth=1.0;
    loadlabelview.layer.borderColor=[UIColor clearColor].CGColor;
    loadlabelview.layer.masksToBounds = YES;
    loadlabelview.layer.cornerRadius = 5;
    [self addSubview:loadlabelview];
    // CGRect ffrm=loadlabelview.frame;
    self.loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 70, 80.0, 20.0)];
    self.loadingLabel.textColor = [UIColor whiteColor];
    //self.loadingLabel.numberOfLines=0;
    self.loadingLabel.backgroundColor = [UIColor clearColor];
    self.loadingLabel.textAlignment=NSTextAlignmentCenter;
    self.loadingLabel.font=[UIFont systemFontOfSize:14];
    //self.loadingLabel.adjustsFontSizeToFitWidth=YES;
    self.loadingLabel.text = @"正在加载...";
    
    [loadlabelview addSubview:self.loadingLabel];
    self.activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(45,20, 32, 32)];
    self.activity.center=CGPointMake(60, 35);
    
    [self.activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [loadlabelview addSubview:self.activity];
    [self.activity startAnimating];
    [superview addSubview:self];
}
-(void)MessageViewWithString:(NSString*)str superView:(UIView*)superview isCancel:(BOOL)isCa
{
    self.frame=superview.frame;
    isCancel=isCa;
    UIView *loadlabelview=[[UIView alloc]initWithFrame:CGRectMake(self.frame.size.width/2-120, self.frame.size.height/2-60, 240.0, 120.0)];
    loadlabelview.backgroundColor=[UIColor blackColor];
    loadlabelview.layer.borderWidth=1.0;
    loadlabelview.layer.borderColor=[UIColor clearColor].CGColor;
    loadlabelview.layer.masksToBounds = YES;
    loadlabelview.layer.cornerRadius = 5;
    [self addSubview:loadlabelview];
    // CGRect ffrm=loadlabelview.frame;
    self.loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 240, 120)];
    self.loadingLabel.textColor = [UIColor whiteColor];
    //self.loadingLabel.numberOfLines=0;
    self.loadingLabel.backgroundColor = [UIColor clearColor];
    self.loadingLabel.textAlignment=NSTextAlignmentCenter;
    self.loadingLabel.font=[UIFont systemFontOfSize:14];
    self.loadingLabel.adjustsFontSizeToFitWidth=YES;
    self.loadingLabel.text = str;
    
    [loadlabelview addSubview:self.loadingLabel];
    //    self.activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(30,44, 32, 32)];
    //
    //    [self.activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    //    [loadlabelview addSubview:self.activity];
    //    [self.activity startAnimating];
    [superview addSubview:self];
    if(isCa)
    {
        [self performSelector:@selector(dismissFromSupview) withObject:nil afterDelay:0.5];
    }
}

-(void)dismissFromSupview
{
    [self removeFromSuperview];
    isCancel=NO;
    self.isloading=NO;
    
}
//-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//{
//    [self removeFromSuperview];
//}

@end
